/**
 * Created by EiT on 2016/12/19.
 */
/* 支付验证 */
function IsChinese(name) {
    var str = $.trim($(name).val());
    if(str == '') {
        salert("对不起，姓名不能为空!");
        return false;
    }else{
        return true;
    }
}
function IsPhone(name) {
    var str = $(name).val();
    var reg =/^1[3,4,5,7,8]\d{9}$/; //是不是手机号码
    if(!reg.test(str)) {
        salert("对不起，您输入的手机号码不正确!");
        return false;
    }else{
        return true;
    }
}
function IsAddress(name) {
    var str = $.trim($(name).val());
    if(str=='') {
        salert("对不起，请您输入地址!");
        return false;
    }else{
        return true;
    }
}
//姓名验证
$("#orderName").on("blur",function(){
    IsChinese("#orderName");
})
//手机号验证
$("#orderPhone").on("blur",function(){
    IsPhone("#orderPhone");
})
//地址标签的隐藏
$("#orderAddress").on("blur",function(){
    IsAddress("#orderAddress");
});

var cart = JSON.parse(localStorage.getItem('cart'));
$.post(SITEURL + 'wechat/shop/submitCart',{cart:cart},function(data){
    if(data.msg=='ok'){
        subOrder(data.data);
    }else{
        salert(data.msg);
    }
});

//提交订单操作
function subOrder(obj){

    console.log(obj);

    if(obj.address){
        $('#orderAddress').val(obj.address);
    }

    if(obj.member_name){
        $('#orderName').val(obj.member_name);
    }
    if(obj.member_phone){
        $('#orderPhone').val(obj.member_phone);
    }

    //设置送餐时间
    $('#orderTime').text(obj.deliver_time);
    $('#orderReginName').text(obj.region_name);
    var couponValue = obj.coupon_value==0?'不使用礼券':'-'+obj.coupon_value+'元';
    $('#couponValue').text(couponValue);


    //设置用户点餐列表
    var orderList = '';
    var menu = obj.menu;
    var l = menu.length;
    for(var i=0;i<l;i++){
        orderList += ('<div class="pay-cont-wp" menuId="'+menu[i].menu_id+'"><h3 class="tit">'+menu[i].menu_week+'<br/>('+menu[i].menu_date+')</h3><dl class="list">');
        var goods = menu[i].goods;
        var gl = goods.length;
        for(var c=0;c<gl;c++){
            orderList += ('<dd><p>'+goods[c].goods_name+'</p><span>x'+goods[c].goods_num+'</span></dd>');
        }
        orderList += ('</dl><span class="pase">'+menu[i].goods_price+'元</span></div>');
    }
    $('#orderGoods').html(orderList);

    $('#orderTotal').text(obj.goods_amount+'元');
    $('#orderDiscount').text('-'+obj.discount_amount+'元');
    $('#orderAmount').text(obj.order_amount+'元');

    //点击支付
    $('.zhi-btn').click(function(){
        if(IsChinese("#orderName")&&IsPhone("#orderPhone")&&IsAddress("#orderAddress")){
            $.ajax({
                url: SITEURL + 'wechat/shop/submitOrder',
                dataType: "json",
                data: {
                    "region_id": obj.region_id,
                    "member_name": $("#orderName").val(),
                    "member_phone": $("#orderPhone").val(),
                    "region_name": obj.region_name,
                    "address": $("#orderAddress").val()
                },
                type: "POST",
                beforeSend: function() { salert("订单支付中"); },
                success: function(data) {
                    if(data.code == 0){
                        alert(1);
                        callpay(data.data);
                    } else {
                        salert(data.msg);
                    }

                },
                error: function() {
                    salert("订单支付失败，请重新支付");
                }
            });
        }
        //下单成功,清除localStorage 也就是你说的cookie
        // localStorage.removeItem("cart");
    });

}

function jsApiCall(data){
    WeixinJSBridge.invoke(
        'getBrandWCPayRequest',
        data,
        function(res){
            WeixinJSBridge.log(res.err_msg);
            alert(res.err_code+res.err_desc+res.err_msg);
            if(res.err_msg == "get_brand_wcpay_request:ok"){
                window.location = SITEURL + 'wechat/shop/finish';
            } else {
                salert("支付取消，请重新支付");
            }
        }
    );
}

function callpay(data){
    if (typeof WeixinJSBridge == "undefined"){
        if( document.addEventListener ){
            document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
        }else if (document.attachEvent){
            document.attachEvent('WeixinJSBridgeReady', jsApiCall);
            document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
        }
    }else{
        jsApiCall(data);
    }
}